const setItem = (key, value) => {
  localStorage.setItem(key, value)
}
const getItem = (key) => {
  localStorage.getItem(key)
}
// 专门用于Satoken的配置，加入过期时间的配置
const setSatoken = (key, value, expire) => {
  let obj = {
    data: value,
    time: Date.now(),
    expire: expire,
  }
  //localStorage 设置的值不能为对象,转为json字符串
  localStorage.setItem(key, JSON.stringify(obj))
}
// 专门用于Satoken的配置，加入过期时间的检测
const getSatoken = (key) => {
  let val = localStorage.getItem(key)
  if (!val) {
    return val
  }
  val = JSON.parse(val)
  if (Date.now() - val.time > val.expire) {
    localStorage.removeItem(key)
    return null
  }
  return val.data
}
export { setItem, getItem, setSatoken, getSatoken }
